package organization;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import person.PersonInfo;

public class Arrays implements Serializable{


	private static final long serialVersionUID = 1L;
	private final File base = new File ("base.out");	
	ArrayList <PersonInfo> personBase = new ArrayList <PersonInfo> ();
	ArrayList <Lesson> lessonBase = new ArrayList <Lesson>();
	ArrayList <Auditorium> auditBase = new ArrayList <Auditorium>();	

	public ArrayList <PersonInfo> getPersonBase (){
		return personBase;
	}
	public void openBase() {
		try {
			try {
				try {
					FileInputStream fis = new FileInputStream(base);
					ObjectInputStream oin = new ObjectInputStream(fis);
					Arrays des = (Arrays) oin.readObject();
					this.lessonBase = des.lessonBase;
					this.personBase = des.personBase;
					this.auditBase = des.auditBase;
				} catch (FileNotFoundException e) {

					saveBase();
					System.out.println("File Created");
					openBase();
				}
			} catch (ClassNotFoundException e) {
				System.out.println("Class open error");
			}

		} catch (IOException e) {
			System.out.println("Fatal Error.");
		}
	}

	public void saveBase() {
		try {
			FileOutputStream fos = new FileOutputStream(base);
			ObjectOutputStream oos = new ObjectOutputStream(fos);

			oos.writeObject(this);
			System.out.println("Base write [OK]");
			oos.flush();
			System.out.println("Base flush [OK]");
			oos.close();
			System.out.println("Base close [OK]");
		} catch (IOException e) {
			System.out.println("Error on write base");
		}
	}
}